//
//  TrainManageSelectView.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/15.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TrainManageSelectView.h"
#import "TrainManageReasonCell.h"
@interface TrainManageSelectView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) UIView *shadowView;
@property (nonatomic,strong) TrainManageReasonCell *cell;
@property (nonatomic,copy) NSArray *dataArr;
@end

@implementation TrainManageSelectView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    [self addSubview:self.shadowView];
    [self reloadData];
}

#pragma mark ==========加载数据==========
- (void)reloadData{
    [[NetworkEngine sharedClient] GETByToken:TRAINMANAGEMENTCANCLEREASONS data:[NSDictionary dictionaryWithObjectsAndKeys:@"1007",@"dictNo", nil] success:^(id responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue] == 1) {
            self.dataArr = [responseObject objectForKey:@"data"];
            [self.tableView reloadData];
        } else if ([[responseObject objectForKey:@"code"] integerValue] == 2) {
            if ([self.delegate respondsToSelector:@selector(trainManageSelectViewDelegateToLoign)]) {
                [self.delegate trainManageSelectViewDelegateToLoign];
            }
        }
    } failure:^(NSString *error) {
        
    }];
}

#pragma mark ==========懒加载==========
- (UIView *)shadowView{
    if (!_shadowView) {
        _shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, -CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        _shadowView.backgroundColor = RGBA(25, 25, 25, 0.5);
    }
    return _shadowView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 140*SCALEUNIT)];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_headerView.frame), 90*SCALEUNIT)];
        title.text = @"取消原因";
        title.textAlignment = NSTextAlignmentCenter;
        title.font = [UIFont systemFontOfSize:LoginFont-1];
        title.textColor = [UIColor colorWithHexString:@"#282828"];
        [_headerView addSubview:title];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), CGRectGetWidth(_headerView.frame), SCALEUNIT)];
        line.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
        [_headerView addSubview:line];
    }
    return _headerView;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 135*SCALEUNIT)];
        UIButton *sure = [self returnButtonWithTitleColor:[UIColor whiteColor] WithBackgroundColor:[UIColor colorWithHexString:@"#21d4a4"] WithTitle:@"确定" WithjudgeNum:1];
        [sure addTarget:self action:@selector(actionChange:) forControlEvents:UIControlEventTouchUpInside];
        sure.tag = 10001;
        [_footerView addSubview:sure];
        
        UIButton *cancle = [self returnButtonWithTitleColor:[UIColor colorWithHexString:@"#21d4a4"] WithBackgroundColor:[UIColor whiteColor] WithTitle:@"取消" WithjudgeNum:2];
        cancle.tag = 10000;
        [cancle addTarget:self action:@selector(actionChange:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:cancle];
    }
    return _footerView;
}

- (UIButton *)returnButtonWithTitleColor:(UIColor *)titleColor WithBackgroundColor:(UIColor *)backColor WithTitle:(NSString *)title WithjudgeNum:(NSInteger)judgeNum{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(judgeNum==1?0:CGRectGetWidth(_headerView.frame)/2.0, 45*SCALEUNIT, CGRectGetWidth(_headerView.frame)/2.0, 90*SCALEUNIT);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-3]];
    [btn setBackgroundColor:backColor];
    return btn;
}
- (UITableView *)tableView{//525*475
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 525*SCALEUNIT, 470*SCALEUNIT) style:UITableViewStylePlain];
        _tableView.center = self.center;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[TrainManageReasonCell class] forCellReuseIdentifier:@"TrainManageReasonCell"];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 525*SCALEUNIT, 470*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(15*SCALEUNIT,15*SCALEUNIT)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = CGRectMake(0, 0, 525*SCALEUNIT, 470*SCALEUNIT);
        maskLayer.path = maskPath.CGPath;
        _tableView.layer.mask = maskLayer;
    }
    return _tableView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TrainManageReasonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TrainManageReasonCell" forIndexPath:indexPath];
    if (self.dataArr != nil) {
        cell.dic = [self.dataArr objectAtIndex:indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TrainManageReasonCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell != self.cell) {
        self.cell.selectJudge = NO;
        cell.selectJudge = YES;
        self.cell = cell;
    }
}

#pragma mark ==========取消+恢复行程==========
- (void)actionChange:(UIButton *)btn{
    if (self.cell==nil&&btn.tag-10000) {
        
    }else {
        [self endAnimation];
    }
    if ([self.delegate respondsToSelector:@selector(trainManageSelectViewDelegateWithReasonId:WithState:)]) {//num==0为取消 否则为确定
        [self.delegate trainManageSelectViewDelegateWithReasonId:[NSNumber numberWithLong:self.cell==nil?0:[[self.cell.dic objectForKey:@"id"] longLongValue]] WithState:btn.tag-10000];
    }
}

#pragma mark ==========开启+结束动画动画==========
- (void)doAnimation{
    [UIView animateWithDuration:0.7f animations:^{
        self.shadowView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [self addSubview:self.tableView];
    }];
}
- (void)endAnimation{
    [self.tableView removeFromSuperview];
    [UIView animateWithDuration:0.7f animations:^{
        self.shadowView.transform = CGAffineTransformMakeTranslation(0, -CGRectGetHeight(self.frame));
    }];
}






@end
